let app = getApp();
var qqmapsdk = require('../../utils/qqmap-wx-jssdk.min.js');
// 实例化API核心类
qqmapsdk = new qqmapsdk({
  key: 'BN3BZ-2KU3D-GQY4H-HBG6C-DV3O2-4RFJX'
});

// pages/trainmap/trainmap.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: 0,
    // markers: [{
    //   id:1,
    //   iconPath: "../../images/index/location.png",
    //   latitude: 23.15792,
    //   longitude: 113.27324,
    //   width: 30,
    //   height: 32,
    //   //气泡callout
    //   callout: {
    //     content: '在这里上车',  //文本
    //     color: '#000000',  //文本颜色
    //     borderRadius: 10,  //边框圆角
    //     borderWidth: 1,  //边框宽度
    //     borderColor: '#e5e7ea',  //边框颜色
    //     bgColor: '#ffffff',  //背景色
    //     padding: 8,  //文本边缘留白
    //     textAlign: 'center',  //文本对齐方式。有效值: left, right, center
    //     display: 'ALWAYS'
    //   },
    // }],
    circles: [{
      latitude: 23.099994,
      longitude: 113.324520,
      radius: 20,
      color: '#D8ECD170',
      fillColor: '#D8ECD170',
    }],
    addSelectFlag: true,
    addressData: [],
    point: 'goPoint',
    goPoint: '',
    nextPoint: '',
    animationData: {},
    address: '',
    recommend: '',
    latitude: '',
    longitude: '',
    city: '',
    lastVal: ''
  },

  handleInputFocus() {
    let that = this;
    that.setData({
      addSelectFlag: false,
      goPoint: ''
    })
  },
  handleInputBlur() {
    let that = this;
    that.setData({
      addSelectFlag: true
    })
  },
  addressSelect(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let infoArray = that.data.addressData[index];
    let val = infoArray.title;
    let location = infoArray.location;
    that.setData({
      latitude: location.lat,
      longitude: location.lng
    });
    if (that.data.point === "goPoint") {
      that.setData({
        goPoint: val,
      })
    } else {
      that.setData({
        nextPoint: val,
      })
      that.okSumbit();
    }
  },
  okSumbit() {
    let _that = this
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    if (_that.data.point === "goPoint") {
      prevPage.setData({
        goPoint: _that.data.goPoint || _that.data.recommend,
        point: _that.data.point,
        latitude: _that.data.latitude,
        longitude: _that.data.longitude,
      })
    } else {
      prevPage.setData({
        nextPoint: _that.data.nextPoint,
        point: _that.data.point,
        latitude: _that.data.latitude,
        longitude: _that.data.longitude,
      })
    }
    wx.navigateBack({
      delta: 1
    })
  },
  //动画 -- 滑入
  slideIn: function () {
    this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: this.animation.export()
    })
  },
  regionchange(e) {
    let that = this;
    if (e.type === 'end') {
      // 地图
      var mapCtx = wx.createMapContext('bus')
      mapCtx.getCenterLocation({
        success: function (res) {
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: res.latitude,
              longitude: res.longitude
            },
            success: function (addressRes) {
              let address = addressRes.result.address; // 描述
              let recommend = addressRes.result.formatted_addresses.recommend; // 描述
              let city = addressRes.result.address_component.city;
              that.setData({
                address: address,
                recommend: recommend,
                city: city,
                goPoint: recommend
              })
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let point = options.point;
    let lastVal = options.lastVal;
    let latitude = options.latitude;
    let longitude = options.longitude;
    let val = point === 'goPoint' ? true : false
    that.setData({
      winHeight: app.globalData.winHeight,
      addSelectFlag: val,
      point: point,
      goPoint: lastVal,
      latitude: latitude,
      longitude: longitude
    });
    that.mapSearch(that.data.lastVal);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  getCenterLocation: function () {
    var that = this
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  },
  returnLoca() {
    let mapCtx = wx.createMapContext('bus');
    mapCtx.moveToLocation();
  },
  mapSearch(val) {
    let that = this;
    // 调用接口
    qqmapsdk.search({
      keyword: val || '商务楼宇酒店',  //搜索关键词
      region: that.data.city,
      location: {
        latitude: that.data.latitude,
        longitude: that.data.longitude
      },  //设置周边搜索中心点
      success: function (res) { //搜索成功后的回调
        that.setData({
          addressData: res.data
        })
      }
    });
  },
  addressChange(e) {
    let that = this;
    let val = e.detail.value;
    that.mapSearch(val);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 1000,//动画的持续时间
      timingFunction: 'ease',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 50);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})